<?php
namespace app\goods\home;


use app\index\controller\Home;
use app\goods\model\Product as ProductModel;
use app\goods\model\Brand as BrandModel;
use app\goods\model\GoodsClass as GoodsClassModel;
use app\goods\model\Color as ColorModel;
use app\goods\model\ColorGroup as ColorGroupModel;
use app\goods\model\SizeGroup as SizeGroupModel;
use app\goods\model\Size as SizeModel;
use app\goods\model\CodeRule as CodeRuleModel;
use app\goods\model\Activites as ActivitesModel;
use app\goods\model\Tag as TagModel;
use app\shop\model\Shop as ShopMfodel;
use think\Db;

/**
* 商品
*/
class Product extends Home
{
	//SKU列表
	public function index(){
		//判断当前分类级别
		$id = '';
		$newid = '';
		$category_list = [];
		$id = input('id');
		if($id > 0){
		$parentid = Db::name('goods_type')->where('typeid', $id)->value('parentid');
		if($parentid > 0){
			$where['b.typeid']   = $id;
			$newid = $parentid;
		}else{
			$where['b.parentid'] = $id;
			$newid = $id;
		}
		

		//类别
		$category_list = Db::name('goods_type')->where(['parentid' => $newid, 'status' => '1'])->field('typeid,classname')->order('sort ASC')->select();
		
	}




		//款列表
		$where['a.status']      = 1;
		$where['a.bedit']       = 1;
		$where['a.checkstatus'] = 1;
		$where['a.isdel']       = 0;
		$where['b.status']      = 1;

		$keyword = '';
		$keyword = input('keyword');
		$key_id  = input('key_id');
		if($key_id > 0){
			$keyword = Db::name('search_keyword')->where('id', $key_id)->value('keyword');
		}
		if($keyword != ''){
			$where['b.classname'] = array('like',"%$keyword%");
			$key_data = [];
			$key_data['keyword'] = $keyword;
			$key_result = Db::name('search_keyword')->where($key_data)->find();
			$key_data['lastupdatetime'] = time();
			if($key_result){
				$key_data['hit'] = $key_result['hit']+1;
				$key_data['id']  = $key_result['id'];
				Db::name('search_keyword')->update($key_data);
			}else{
				$key_data['inputtime'] = time();
				Db::name('search_keyword')->insert($key_data);
			}
		}

		$where['c.status']      = 1;
		

		//查所有款 逆推类别
		if($keyword != ''){
			$class_all = Db::name('goods_list a')
				   ->join('goods_class b', 'a.classid=b.classid', 'left')
				   ->join('goods_activites c', 'a.activitesid=c.activitesid', 'left')
				   ->field('b.typeid')
				   ->where($where)
				   ->order('b.sort ASC')
				   ->group('b.classid')
				   ->select();
			$class_ids = [];
			
			foreach ($class_all as $key => $value) {
				$class_ids[] = $value['typeid'];
			}
			$category_where['typeid'] = array('in', $class_ids);
			$category_list = Db::name('goods_type')->where($category_where)->field('typeid,classname')->order('sort ASC')->select();
		}

		$this->assign('newid', $newid);
		$this->assign('id',$id);
		$this->assign('category_list', $category_list);
		$this->assign('keyword',$keyword);

		//SKU列表(逆推品牌/色系) 
		$sku_list = Db::name('goods_list a')
				   ->join('goods_class b', 'a.classid=b.classid', 'left')
				   ->join('goods_activites c', 'a.activitesid=c.activitesid', 'left')
				   ->join('admin_attachment d', 'd.id=b.thumb', 'left')
				   ->field('a.brandid,a.colorgroupid')
				   ->where($where)
				   ->select();
		$brandids      = [];
		$colorgroupids = [];
		foreach ($sku_list as $key => $value) {
			$brandids[] = $value['brandid'];
			$colorgroupids[] = $value['colorgroupid'];
		}

		//品牌列表
		$brand_where['brandid'] = array('in',$brandids); 
		$brand_where['status']  = 1;
		$brand_list = Db::name('goods_brand')->where($brand_where)->field('brandid,brandname')->order('sort ASC')->select();
		$this->assign('brand_list', $brand_list);

		//色系列表
		$colorgroup_where['colorgroupid'] = array('in',$colorgroupids);
		$colorgroup_where['status']       = 1;
		$colorgroup_list = ColorGroupModel::where($colorgroup_where)->field('colorgroupid,groupname,colorgroupvalue1,colorgroupvalue2')->order('sort ASC')->select();
		$this->assign('colorgroup_list', $colorgroup_list);

		//属性筛选条件
		$attr_list = [];
		$attr_list = Db::name('goods_attr')->where(['status' => '1', 'type' => '1'])->order('sort ASC')->select();
		foreach ($attr_list as $key => $value) {
			$attr_list[$key]['list'] = explode(',', trim($value['typedetail'], ','));
		}
		$this->assign('attr_list',$attr_list);


		//左侧导航
		$this->left_category();

		return $this->fetch('index');
	}


	//SKU详情
	public function goods_detail($id = 20){
		//Request::instance()->session();
		//dump(input('session.'));
		if($id == 0){
			$this->error('参数错误');
		}
		$type = '0';
		$type = input('type');
		//款详细
		$class_info = Db::name('goods_class')->where('classid', $id)->find();
		$class_info['parent_type'] = Db::name('goods_type')->where('typeid',$class_info['parentid'])->value('classname');
		$class_info['type'] = Db::name('goods_type')->where('typeid',$class_info['typeid'])->value('classname');

		//关联款
		$about_class = [];
		$about_where['b.classid'] = array('in',$class_info['carrelation']);
		$about_where['a.status']      = 1;
		$about_where['a.bedit']       = 1;
		$about_where['a.checkstatus'] = 1;
		$about_where['a.isdel']       = 0;
		$about_where['b.status']      = 1;
		$about_where['c.status']      = 1;

		$about_class = Db::name('goods_list a')
				   ->join('goods_class b', 'a.classid=b.classid', 'left')
				   ->join('goods_activites c', 'a.activitesid=c.activitesid', 'left')
				   ->join('admin_attachment d', 'd.id=b.thumb', 'left')
				   ->field('b.classname,b.artno,d.path thumb,b.classid,c.type,b.tagid')
				   ->where($about_where)
				   ->order('b.sort ASC')
				   ->group('c.type,b.classid')
				   ->select();
		$TagModel = new TagModel;
		foreach ($about_class as $key => $value) {
			$about_class[$key]['tag'] = $TagModel->getTags($value['tagid']);
		}
		$this->assign('about_class',$about_class);

		//属性值列表
		$attrval_list = Db::name('goods_attrval a')
					  ->join('goods_attr b', 'a.attrid=b.attrid', 'left')
					  ->where('a.classid='.$id.' and b.status=1')
					  ->order('b.sort ASC')
					  ->field('a.detail,b.attrname')
					  ->select();
		foreach ($attrval_list as $key => $value) {
			$attrval_list[$key]['detail'] = trim($value['detail'], ',');
		}
		//SKU列表
		if(!empty($type)){
			$goods_where['e.type'] = $type;
		}

		$goods_list = Db::name('goods_list a')
					->join('goods_color b', 'a.colorid=b.colorid', 'left')
					->join('shop c', 'a.ownerid=c.id', 'left')
					->join('goods_brand d', 'a.brandid=d.brandid', 'left')
					->join('goods_activites e', 'a.activitesid=e.activitesid', 'left')
					->where(['a.status' => '1', 'a.bedit' => '1', 'a.checkstatus' => '1', 'a.isdel' => '0', 'a.classid' => $id, 'b.status' => '1','c.status' => '1','d.status' => '1', 'e.type' => $type])
					->order('b.sort ASC')
					->group('b.colorid')
					->field('a.goodsid,a.goodsname,a.goodsno,a.conversion,a.front,a.opposite,a.side,b.name colorname,b.colorvalue1,b.colorvalue2,b.customcode colorcustomcode,b.colorid,c.name shopname,c.id shopid,d.brandname,e.type')
					->select();
		foreach ($goods_list as $key => $value) {
			$goods_list[$key]['front']    = $this->getPath($value['front']);
			$goods_list[$key]['opposite'] = $this->getPath($value['opposite']);
			$goods_list[$key]['side']     = $this->getPath($value['side']);
		}
		$this->assign('class_info', $class_info);
		$this->assign('attrval_list', $attrval_list);
		$this->assign('goods_list', $goods_list);


		//仓库列表
		$warehouse_list = Db::name('warehouse_list')->where('status','1')->field('warehouseid,code,warehousename')->select();
		$this->assign('warehouse_list', $warehouse_list);


		//省份
		$city_list = Db::name('admin_area')->where(['status' => '1','pid' => '0'])->order('listorder ASC')->select();

	
		$iplocation   = '';
		$provnce_list = '';
		foreach ($city_list as $key => $value) {
			$iplocation   .= '"'.$value['name'].'": { id: "'.$value['id'].'", root: 0, djd: 1,c:72 },';
			$provnce_list .= '<li><a data-value="'.$value['id'].'">'.$value['name'].'</a></lihref="#none" >';
		}
		$this->assign('provnce_list', $provnce_list);
		$this->assign('iplocation',$iplocation);

		
		//左侧导航
		$this->left_category();

		return $this->fetch('goods_detail');
	}


	//筛选
	public function screen(){
		$this->view->engine->layout(false);
		$data  = input('');
		$color = [];
		$brand = [];
		$attr  = [];
		$class = [];
		$keyword  = '';
		$parentid = '';
		$tag   = '';
		$page  = 1;
		foreach ($data['all_select'] as $key => $value) {
			if(strstr($value, 'color_')){
				$color[] = trim($value,'color_');
			}
			if(strstr($value, 'brand_')){
				$brand[] = trim($value,'brand_');
			}
			if(strstr($value, 'attr_')){
				$attr[] = $value;
			}
			if(strstr($value, 'class_')){
				$class[] = trim($value,'class_');
			}
			if(strstr($value, 'category_')){
				$parentid = trim($value,'category_');
			}
			if(strstr($value, 'tag')){
				$tag = trim($value,'tag_');
			}
			if(strstr($value, 'page')){
				$page = trim($value,'page_');
			}
			if(strstr($value, 'keyword')){
				$keyword = trim($value,'keyword_');
			}
		}
		if(!empty($color)){
			$where['a.colorgroupid'] = array('in',$color);
		}
		if(!empty($class)){
			$where['b.typeid'] = array('in',$class);
		}
		if(!empty($brand)){
			$where['a.brandid'] = array('in',$brand);
		}

		$attr_ids = [];
		foreach ($attr as $key => $value) {
			$attr_data = explode('_', $value);
			$attr_where['attrid'] = $attr_data['1'];
			$attr_where['detail'] = array('like', "%$attr_data[2]%");
			$attr_list          = Db::name('goods_attrval')->where($attr_where)->select();
			foreach ($attr_list as $key => $value) {
				$attr_ids[] = $value['classid'];
			}
		}
		if(!empty($attr_ids)){
			$where['b.classid'] = array('in',$attr_ids);
		}
		if($tag > 0){
			$where['b.tagid'] = array('like', "%,$tag,%");
		}
		if($parentid > 0){
			$where['b.parentid']      = $parentid;
		}
		if($keyword != ''){
			$where['b.classname'] = array('like', "%$keyword%");
		}
		


		//款列表
		$where['a.status']      = 1;
		$where['a.bedit']       = 1;
		$where['a.checkstatus'] = 1;
		$where['a.isdel']       = 0;
		$where['b.status']      = 1;
		$where['c.status']      = 1;
		$where['e.status']      = 1;

		//统计总数
		$data_count = Db::name('goods_list a')
				   ->join('goods_class b', 'a.classid=b.classid', 'left')
				   ->join('goods_activites c', 'a.activitesid=c.activitesid', 'left')
				   ->join('admin_attachment d', 'd.id=b.thumb', 'left')
				   ->join('goods_type e', 'b.typeid=e.typeid', 'left')
				   ->field('b.classname,b.artno,d.path thumb,b.classid,c.type')
				   ->where($where)
				   ->order('b.sort ASC')
				   ->group('c.type,b.classid')
				   ->count();
		$this->assign('data_count', $data_count);


		//分组查询
		$data_list = Db::name('goods_list a')
				   ->join('goods_class b', 'a.classid=b.classid', 'left')
				   ->join('goods_activites c', 'a.activitesid=c.activitesid', 'left')
				   ->join('admin_attachment d', 'd.id=b.thumb', 'left')
				   ->join('goods_type e', 'b.typeid=e.typeid', 'left')
				   ->field('b.classname,b.artno,d.path thumb,b.classid,c.type,b.tagid')
				   ->where($where)
				   ->order('b.sort ASC')
				   ->group('c.type,b.classid')
				   ->paginate(10,false,['type' => 'Productpage', 'var_page' => 'page', 'list_rows' => 15, 'newstyle' => true,'page' => $page]);
		$this->assign('data_page', $data_list);
		// 获取分页显示
		$page = $data_list->render();
		// 模板变量赋值
		$data_list = $data_list->all();
		$TagModel = new TagModel;
		foreach ($data_list as $key => $value) {
			$data_list[$key]['tag'] = [];
			$data_list[$key]['tag'] = $TagModel->getTags($value['tagid']);
		}
		$this->assign('page', $page);
		$this->assign('data_list', $data_list);

		//标签列表
		$tag_list = TagModel::all();
		$this->assign('tag_list', $tag_list);
		$this->assign('tag',$tag);
		
		return $this->fetch('screen');
	}

	public function price(){
		$this->view->engine->layout(false);
		$uid = session('uid');
		if(empty($uid)){
			return false;
		}

		//仓库
		$warehouseid    = input('warehouseid');
		$warehouse_info = Db::name('warehouse_list')->where('warehouseid', $warehouseid)->find();
		$this->assign('warehouse_info',$warehouse_info);

		//获取用户价格等级
		
		$member_level = Db::name('member')->where('id',$uid)->value('member_level');
		$price_level  = Db::name('member_level')->where('id',$member_level)->value('level_price');

		//等级code
		$level_code   = Db::name('member_level')->where('id',$member_level)->value('code');
		$this->assign('level_code',$level_code);

		$colorid = input('colorid');
		$classid = input('classid');
		$type    = input('type');

		//颜色
		$color   = Db::name('goods_color')->where('colorid',$colorid)->find();
		$this->assign('color', $color);

		$first_td = ['name' => $color,'money' => '单价（元）', 'goodsid' => '仓库', 'number' => '购买数量'];
		$this->assign('first_td', $first_td);

		$where['a.colorid'] = $colorid;
		$where['a.classid'] = $classid;
		$where['a.status']  = 1;
		$where['a.bedit']   = 1;
		$where['a.checkstatus'] = 1;
		$where['a.isdel']   = 0;
		$where['b.status']  = 1;
		$where['c.type']    = $type;
		$time               = time();
		$where['e.status']  = 1;
		$where['e.start_time'] = array('elt',$time);
		$where['e.end_time'] = array('egt',$time);
		$where['d.levelid']  = $price_level;
		$size_list = Db::name('goods_size b')
					->join('goods_list a', 'a.sizeid=b.sizeid', 'left')
					->join('goods_activites c', 'a.activitesid=c.activitesid', 'left')
					->join('goods_price d', 'a.goodsid=d.skuid', 'left')
					->join('goods_pricegroup e', 'e.pricegroupid=d.pricegroupid', 'left')
					->where($where)
					->field('a.goodsid,a.colorid,b.sizeid,b.name,d.money')
					->order('b.sort ASC')
					->select();
		//库存
		foreach ($size_list as $key => $value) {
			$size_list[$key]['waitnum'] = Db::name('goods_stock')->where(['skuid' => $value['goodsid'], 'warehouseid' => $warehouseid])->value('waitnum');
		}
		$this->assign('size_list',$size_list);

		//会员等级
		$level_list = Db::name('member_level')->where('status', '1')->order('sort ASC')->select();
		foreach ($level_list as $key => $value) {
			$where['d.levelid'] = $value['level_price'];
			$level_list[$key]['list'] = Db::name('goods_size b')
					->join('goods_list a', 'a.sizeid=b.sizeid', 'left')
					->join('goods_activites c', 'a.activitesid=c.activitesid', 'left')
					->join('goods_price d', 'a.goodsid=d.skuid', 'left')
					->join('goods_pricegroup e', 'e.pricegroupid=d.pricegroupid', 'left')
					->where($where)
					->field('a.goodsid,a.colorid,b.sizeid,b.name,d.money')
					->order('b.sort ASC')
					->select();
		}

		$this->assign('level_list',$level_list);

		return $this->fetch('price');
	}


	//收藏
	public function collection(){
		$data['memberid'] = session('uid');
		$data['classid']  = input('classid');
		$data['colorid']  = input('colorid');
		$data['shopid']   = input('shopid');
		$data['type']     = input('type');

		//判断参数
		if(empty($data['memberid']) || empty($data['classid']) || empty($data['colorid'])){
			$msg_data['code'] = '1';
			$msg_data['msg']  = '参数错误';
			return json($msg_data);
		}

		//判断是否已收藏
		$data['is_del'] = '0';
		$result = Db::name('member_collection')->where($data)->find();

		if($result){
			$msg_data['code'] = '2';
			$msg_data['msg']  = '你已收藏该商品';
			return $msg_data;
		}

		//执行收藏
		$data['create_time'] = time();
		$res = Db::name('member_collection')->insert($data);
		if($res){
			$msg_data['code'] = '3';
			$msg_data['msg']  = '收藏成功';
			return $msg_data;
		}else{
			$msg_data['code']  = '4';
			$msg_data['msg']   = '收藏失败';
			return $msg_data;
		}
	}

	//查看是否已收藏
	public function has_collection(){
		if(!$this->mid){
			$data['code'] = 0;
		    echo json_encode($data);
			return false;
		}
		$where['colorid'] = input('colorid');
		$where['type']    = input('type');
		$where['classid'] = input('classid');
		$where['is_del']  = 0;
		$where['memberid']= $this->mid;
		$result = Db::name('member_collection')->where($where)->find();
		if($result){
			$data['code'] = 1;
		}else{
			$data['code'] = 0;
		}
		echo json_encode($data);
	}
}


?>